﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace Frame.Lib.Core.Tool
{
    public class GenerateCode
    {
        private SysTable _sysTable;
        private string _className;
        private string _tableName;

        public GenerateCode(string connectionString,string tableName,string className)
        {
            _sysTable = new SysTable(connectionString, tableName);
            _tableName = tableName;

             _className = className;
        }

      

        public string GetInfo()
        {
            string strSql = "select * from " + _tableName + " where 1=2";
            DataTable dt = _sysTable.GetSchema();
            List<ColumnInfo> tblcols = _sysTable.GetColumns();

            if (_className == "")
            {
                _className = _tableName.TrimStart('[').TrimEnd(']');
                _className = _className.Substring(0, 1).ToUpper() + _className.Substring(1);
                _className = _className + "Info";

                int index = _className.IndexOf('_');
                if (index > -1)
                {
                    _className = _className.Substring(index + 1, 1).ToUpper() + _className.Substring(index + 2);
                }
            }

            StringBuilder sbClass = new StringBuilder();
            sbClass.Append("public partial class " + _className + ":EntityBase\r\n");
            sbClass.Append("{\r\n");

            StringBuilder strUpdate = new StringBuilder();
            StringBuilder strInsertField = new StringBuilder();
            StringBuilder strInsertValue = new StringBuilder();
            StringBuilder strProperty = new StringBuilder();

            StringBuilder strSetValueSwitch = new StringBuilder();//SetValue方法的Switch部分
            StringBuilder strGetValueSwitch = new StringBuilder();

            DataColumnCollection columns = dt.Columns;

            foreach (DataColumn col in columns)
            {
                string upperColumnName = col.ColumnName.Substring(0, 1).ToUpper() + col.ColumnName.Substring(1);

                if (upperColumnName.ToUpper() != "ID")
                {
                    strUpdate.Append(", [" + upperColumnName + "] = @" + upperColumnName);
                }
                strInsertField.Append(", [" + upperColumnName + "]");
                strInsertValue.Append(", @" + upperColumnName);

                Type dataType = col.DataType;
                string strDataType = dataType.Name;
                //TypeCode.
                ColumnInfo info = tblcols.Where(tmp => tmp.Name.ToLower() == upperColumnName.ToLower()).First();
                if (strDataType != typeof(string).Name && info.IsNullable == 1)
                {
                    strDataType += "?";
                }
                strProperty.Append("\t/// <summary>\r\n");
                strProperty.Append("\t/// " + info.Description + "\r\n");
                strProperty.Append("\t/// </summary>\r\n");
                strProperty.Append("\tpublic " + strDataType + " " + upperColumnName + "\r\n");
                strProperty.Append("\t{\r\n");
                strProperty.Append("\t\tget;\r\n");
                strProperty.Append("\t\tset;\r\n");
                strProperty.Append("\t}\r\n");

                strSetValueSwitch.Append("\t\t\tcase \"" + upperColumnName.ToLower() + "\":\r\n");
                strSetValueSwitch.Append("\t\t\t\tthis." + upperColumnName + " = (" + strDataType + ")value;\r\n");
                strSetValueSwitch.Append("\t\t\t\tbreak;\r\n");

                strGetValueSwitch.Append("\t\t\tcase \"" + upperColumnName.ToLower() + "\":\r\n");
                strGetValueSwitch.Append("\t\t\t\treturn this." + upperColumnName + ";\r\n");
                //strGetValueSwitch.Append("\t\t\t\tbreak;\r\n");
            }
            sbClass.Append("\tpublic override string GetTableName()\r\n");
            sbClass.Append("\t{\r\n");
            sbClass.Append("\t\t\treturn \"" + _tableName + "\";\r\n");
            sbClass.Append("\t}\r\n");

            sbClass.Append("\tpublic override string GetInsertSql()\r\n");
            sbClass.Append("\t{\r\n");
            sbClass.Append("\t\t\treturn " + "\"insert into " + _tableName + " (" + strInsertField.ToString().TrimStart(',') + ") values (" + strInsertValue.ToString().Substring(1) + ")\";\r\n");
            sbClass.Append("\t}\r\n");

            sbClass.Append("\tpublic override string GetUpdateSql()\r\n");
            sbClass.Append("\t{\r\n");
            sbClass.Append("\t\t\treturn " + "\"update " + _tableName + " set " + strUpdate.ToString().TrimStart(',') + " where ID = @ID\";\r\n");
            sbClass.Append("\t}\r\n");

            //sbClass.Append("\tpublic " + _className + "(){}\r\n");//构造函数
            sbClass.Append(strProperty);

            //加入SetValue
            sbClass.Append("\tpublic override void SetValue(string key, object value)\r\n");
            sbClass.Append("\t{\r\n");
            sbClass.Append("\t\tkey = key.ToLower();\r\n");
            sbClass.Append("\t\tswitch (key)\r\n");
            sbClass.Append("\t\t{\r\n");
            sbClass.Append(strSetValueSwitch);
            sbClass.Append("\t\t}\r\n");
            sbClass.Append("\t}\r\n");

            //加上GetValue
            sbClass.Append("\tpublic override object GetValue(string key)\r\n");
            sbClass.Append("\t{\r\n");
            sbClass.Append("\t\tkey = key.ToLower();\r\n");
            sbClass.Append("\t\tswitch (key)\r\n");
            sbClass.Append("\t\t{\r\n");
            sbClass.Append(strGetValueSwitch);
            sbClass.Append("\t\t\tdefault:\r\n");
            sbClass.Append("\t\t\t\treturn null;\r\n");
            sbClass.Append("\t\t}\r\n");
            sbClass.Append("\t}\r\n");

            sbClass.Append("}\r\n");

            return sbClass.ToString();
        }

       
        public string GetDAL()
        {   
            if (_className == "")
            {
                _className = _tableName.TrimStart('[').TrimEnd(']');
                _className = _className.Substring(0, 1).ToUpper() + _className.Substring(1);
                _className = _className + "DAL";
                int index = _className.IndexOf('_');
                if (index > -1)
                {
                    _className = _className.Substring(index + 1, 1).ToUpper() + _className.Substring(index + 2);
                }
            }

            StringBuilder sbClass = new StringBuilder();
            sbClass.Append("public class " + _className + ":BaseDAL\r\n");
            sbClass.Append("{\r\n");
          
            sbClass.Append("}\r\n");

            return sbClass.ToString();
        }

        public string GetBO()
        {

            if (_className == "")
            {
                _className = _tableName.TrimStart('[').TrimEnd(']');
                _className = _className.Substring(0, 1).ToUpper() + _className.Substring(1);
                _className = _className + "BO";
                int index = _className.IndexOf('_');
                if (index > -1)
                {
                    _className = _className.Substring(index + 1, 1).ToUpper() + _className.Substring(index + 2);
                }
            }

            string strInfoName = _className.TrimEnd('B','O') + "Info";
            string strDALName = _className.TrimEnd('B', 'O') + "DAL";

            string strField = "_" + _className.Substring(0, 1).ToLower() + _className.TrimEnd('B','O').Substring(1);

            StringBuilder sbClass = new StringBuilder();
            sbClass.Append("public class " + _className + ":CommonOperate<" + strInfoName + ">\r\n");
            sbClass.Append("{\r\n");
            sbClass.Append("\tprivate " + strDALName + " " + strField + ";\r\n");
            sbClass.Append("\tpublic " + _className + "()\r\n");
            sbClass.Append("\t{\r\n");
            sbClass.Append("\t\t" + strField + "= new " + strDALName + "();\r\n");
            sbClass.Append("\t}\r\n");

            sbClass.Append("\tprotected override BaseDAL DAL\r\n");
            sbClass.Append("\t{\r\n");
            sbClass.Append("\t\tget\r\n");
            sbClass.Append("\t\t{\r\n");
            sbClass.Append("\t\t\treturn " + strField + ";\r\n");
            sbClass.Append("\t\t}\r\n");
            sbClass.Append("\t}\r\n");

           

            sbClass.Append("}");

            return sbClass.ToString();
        }
    }
}
